<?php

namespace app\logic;

/**
 * 工具类
 * Class L_tool
 * @package app\logic
 */
class L_tool
{
    /**
     * 替换网站地址
     * @param string $paramObj
     */
    public function replaceSiteUrl($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'old_url' => 'require',
                'new_url' => 'require',
            );
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            $setting=app("cmsTable")::setting();
            $module=app("cmsTable")::module();
            $category=app("cmsTable")::category();

            $tables=array(
                $setting=>"itemValue",
                $module=>"linkUrl",
                $category=>"thumb",
            );
            try {
                foreach($tables as $key=>$val){
                    $update_sql="update {$key} set {$val}=REPLACE({$val}, '{$dataObj->request->old_url}', '{$dataObj->request->new_url}')";
                    Db()->execute($update_sql);
                }

                //获取文章模型
                $paramsObj=paramsObj();
                $paramsObj->params=array(
                    "module"=>"article",
                    "field"=>"id",
                );
                $checkResult=app("S_Admin_module")->allData($paramsObj);
                if($checkResult["error_no"]=="0"){
                    $modules=$checkResult["result"];
                    if($modules){
                        foreach($modules as $module){
//                            //替换文章内容里的图片
//                            $articleData=app("cmsTable")::articleData($module['id']);
//                            $article_data_sql="update {$articleData} set articleContent=REPLACE(articleContent, '{$dataObj->request->old_url}', '{$dataObj->request->new_url}')";
//                            Db()->execute($article_data_sql);

                            //替换文章缩略图
                            $article=app("cmsTable")::article($module['id']);
                            $article_sql="update {$article} set thumb=REPLACE(thumb, '{$dataObj->request->old_url}', '{$dataObj->request->new_url}')";
                            Db()->execute($article_sql);
                        }
                    }
                }
            } catch (\Throwable $e) {
                $final_arr = recordLogMsgLevelTwo(app("errorLevel")::E_ERROR, app("errorCode")::SQL_ERROR, lang("sys.error"), $e->getMessage());
            }
        }
        return $final_arr;
    }
}